package cn.ztuo.bitrade.service;

import cn.ztuo.bitrade.dao.AwardsSettingDao;
import cn.ztuo.bitrade.dao.LotteryDao;
import cn.ztuo.bitrade.dao.MemberDao;
import cn.ztuo.bitrade.dao.WinningRecordDao;
import cn.ztuo.bitrade.entity.Lottery;
import cn.ztuo.bitrade.entity.Member;
import cn.ztuo.bitrade.entity.QLottery;
import cn.ztuo.bitrade.entity.WinningRecord;
import cn.ztuo.bitrade.service.Base.BaseService;
import cn.ztuo.bitrade.util.NumberUtils;
import cn.ztuo.bitrade.util.PredicateUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Xiazt
 * @date: 2020-04-23 09:54
 */
@Service
public class LotteryService extends BaseService {

    @Autowired
    private LotteryDao lotteryDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private AwardsSettingDao awardsSettingDao;

    @Autowired
    private WinningRecordDao winningRecordDao;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public Lottery findByNumber(String number) {
        return lotteryDao.findByNumber(number);
    }

    public Lottery findByPeriods(String periods) {
        return lotteryDao.findByPeriods(periods);
    }

    public Page<Lottery> getByPage(Lottery lottery) {
        ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();
        if (StringUtils.isNotEmpty(lottery.getPeriods())) {
            booleanExpressions.add(QLottery.lottery.periods.like("%" + lottery.getPeriods() + "%"));
        }
        if (StringUtils.isNotEmpty(lottery.getNumber())) {
            booleanExpressions.add(QLottery.lottery.number.like("%" + lottery.getNumber() + "%"));
        }
        Predicate predicate = PredicateUtils.getPredicate(booleanExpressions);
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = new PageRequest(lottery.getPageNum() - 1, lottery.getPageSize(), sort);
        return lotteryDao.findAll(predicate, pageable);
    }

    @Transactional
    public List<WinningRecord> saveOrUpdate(Lottery lottery) {
        Lottery resultLottery = lotteryDao.save(lottery);
        // 本次开奖的7组数字
        List<String> number = NumberUtils.split(resultLottery.getNumber());
        // 所有用户的幸运码和本次开奖的数字比较
        List<Member> memberList = memberDao.findAll();
        List<WinningRecord> winningMember = new ArrayList<>();
        for (Member member : memberList) {
            if (member.getLuckyCode() != null) {
                Integer redRightCount = 0;
                Integer blueRightCount = 0;
                String luckyCode = member.getLuckyCode();
                List<String> memberNumber = NumberUtils.split(luckyCode);
                // 先比较红球
                for (int i = 0; i < memberNumber.size() - 1; i++) {
                    for (int j = 0; j < number.size() - 1; j++) {
                        // 如果相等，加到数组中
                        if (memberNumber.get(i).equals(number.get(j))) {
                            ++redRightCount;
                        }
                    }
                }
                // 最后比较篮球
                if (memberNumber.get(memberNumber.size() - 1).equals(number.get(number.size() - 1))) {
                    ++blueRightCount;
                }
                // 对的球总数
                Integer rightCount = redRightCount + blueRightCount;
                WinningRecord winningRecord = new WinningRecord();
                // 判定为几等奖
                if (rightCount == 7) {
                    winningRecord.setAwardsSetting(awardsSettingDao.findByLevel(0));
                } else if (rightCount == 6) {
                    winningRecord.setAwardsSetting(awardsSettingDao.findByLevel(1));
                } else if (rightCount == 5) {
                    winningRecord.setAwardsSetting(awardsSettingDao.findByLevel(2));
                } else if (rightCount == 4) {
                    winningRecord.setAwardsSetting(awardsSettingDao.findByLevel(3));
                } else if (blueRightCount == 1) {
                    winningRecord.setAwardsSetting(awardsSettingDao.findByLevel(4));
                }

                // 保存中奖信息
                if (winningRecord != null && winningRecord.getAwardsSetting() != null) {
                    winningRecord.setMember(member);
                    winningRecord.setLottery(resultLottery);
                    winningRecord.setStatus(0);
                    WinningRecord resultWinningRecord = winningRecordDao.save(winningRecord);
                    winningMember.add(resultWinningRecord);
                    kafkaTemplate.send("award-bonus", resultWinningRecord.getId().toString(), "发放奖金");
                }
            }
        }
        return winningMember;
    }


    public void delete(Long id) {
        lotteryDao.delete(id);
    }

}
